﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ApplicationModel;
using XWord.Common;
using Microsoft.Practices.ServiceLocation;
using Caliburn.PresentationFramework.ApplicationModel;
using XWord.Fundamental.Domain.Model;
using System.Threading;
using XWord.Fundamental.Domain;
using Caliburn.Core.Metadata;
using System.Collections.ObjectModel;
using Caliburn.PresentationFramework;
using XWord.WPF.ApplicationFramework;
using XWord.GUI.Presenters.Fundamental;
namespace XWord.GUI.Presenters
{



    [Singleton(typeof(IEtymaLibraryMasterScreen))]
    public class EtymaLibraryMasterPresenter : Presenter, IEtymaLibraryMasterScreen
    {
        private readonly IWindowManager _windowManager;
        private readonly IServiceLocator _serviceLocator;
        public EtymaLibraryMasterPresenter(IServiceLocator entityFactory)    
        {
            _serviceLocator = entityFactory;
            _windowManager = entityFactory.GetInstance<IWindowManager>();
            Model = _serviceLocator.GetInstance<IEtymaLibraryModel>();  
        }

        public IEtymaLibraryModel Model { get; set; }

        protected override void OnInitialize()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
            {
                AllEtymaLibraryList = Model.LoadEtymaLibraryList().OrderBy(e => e.Name).ToObservableCollection();
                //PrefixEtymaList = Model.GetPreFix().Distinct().OrderBy(e => e.Name).ToObservableCollection();
                //SuffixEtymaList = Model.GetSuffix().Distinct().OrderBy(e => e.Name).ToObservableCollection();
            }));
            
            base.OnInitialize();
        }

        public IObservableCollection<EtymaMessage> _suffixEtymaList;
        public IObservableCollection<EtymaMessage> SuffixEtymaList
        {
            get { return _suffixEtymaList ?? (_suffixEtymaList = new BindableCollection<EtymaMessage>()); }
            set
            {
                _suffixEtymaList = value;
                NotifyOfPropertyChange("SuffixEtymaList");
            }
        }



        public IObservableCollection<EtymaMessage> _prefixEtymaList;
        public IObservableCollection<EtymaMessage> PrefixEtymaList
        {
            get { return _prefixEtymaList ?? (_prefixEtymaList = new BindableCollection<EtymaMessage>()); }
            set
            {
                _prefixEtymaList = value;
                NotifyOfPropertyChange("PrefixEtymaList");
            }
        }

        public IObservableCollection<EtymaMessage> _bindableprefixEtymaList;
        public IObservableCollection<EtymaMessage> BindablePrefixEtymaList
        {
            get { return _bindableprefixEtymaList ?? (_bindableprefixEtymaList = new BindableCollection<EtymaMessage>()); }
            set
            {
                _bindableprefixEtymaList = value;
                NotifyOfPropertyChange("BindablePrefixEtymaList");
            }
        }

        public IObservableCollection<EtymaMessage> _etymaMessageList;
        public IObservableCollection<EtymaMessage> EtymaMessageList
        {
            get { return _etymaMessageList ?? (_etymaMessageList = new BindableCollection<EtymaMessage>()); }
            set
            {
                _etymaMessageList = value;
                NotifyOfPropertyChange("EtymaMessageList");
            }
        }

        public IObservableCollection<EtymaLibrary> _alletymaLibraryList;
        public IObservableCollection<EtymaLibrary> AllEtymaLibraryList
        {
            get { return _alletymaLibraryList ?? (_etymaLibraryList = new BindableCollection<EtymaLibrary>()); }
            set
            {
                _alletymaLibraryList = value;
                NotifyOfPropertyChange("AllEtymaLibraryList");
            }
        }

        public IObservableCollection<EtymaLibrary> _etymaLibraryList;
        public IObservableCollection<EtymaLibrary> EtymaLibraryList
        {
            get { return _etymaLibraryList ?? (_etymaLibraryList = new BindableCollection<EtymaLibrary>()); }
            set
            {
                _etymaLibraryList = value;
                NotifyOfPropertyChange("EtymaLibraryList");
            }
        }

        private EtymaLibrary _selectedItem;
        public EtymaLibrary SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                NotifyOfPropertyChange("SelectedItem");
            }
        }

        #region edit

        public void OpenNewDialog()
        {
            OpenDialog(false);
        }

        public void OpenEditDialog()
        {
            OpenDialog(true);
        }

        private void OpenDialog(bool IsEdit)
        {
            //var presenter = _serviceLocator.GetInstance<EtymaLibraryEditPresenter>();
            //this.ActiveScreen = presenter;
            //if (IsEdit)
            //{
            //    presenter.Entity = SelectedItem;
            //    presenter.InitializeEditEntity();
            //}
            //else
            //{
            //    presenter.InitializeNewEntity();
            //}
            //_windowManager
            //    .ShowDialog(presenter, null, ExecuteShutdownModel);
        }

        public void Delete()
        {
            Model.Delete(SelectedItem);
            EtymaLibraryList.Remove(SelectedItem);

        }

        #endregion
    }

   
}
